<?php

/**
 * pd-service.php
 *
 * Provide the functions and database operations
 *
 * @package RD
 */


/**
 * Get the category name by the specific id
 */
function get_category_name($categoryid='')
{
	global $rd_db;

	$category_result = $rd_db->get_row("SELECT name_zh_CN FROM rd_category where id = $categoryid and enable = true");

	return $category_result['name_zh_CN'];
}

/**
 * Get the products list by the given category id
 * @param unknown_type $category_id
 */
function get_product_list($categoryid,$page_offset,$page_size)
{
	global $rd_db;
	$categoryid = "'".$categoryid."%'";
	return $rd_db->get_results("SELECT p.* FROM rd_products p,rd_category c where p.category_id = c.id and c.id like $categoryid and p.enable = true order by id limit $page_offset, $page_size");
}

function get_product_count($categoryid)
{
	global $rd_db;
	
	if(!empty($categoryid))
	{
		$categoryid = "'".$categoryid."%'";
		$where .= 'category_id like '.$categoryid.' and enable = true';
	}
	
	return get_row_count('rd_products',$where);
}

/**
 * Get the product by the specific id
 */
function get_product($productid='')
{
	global $rd_db;

	return $rd_db->get_row("SELECT * FROM rd_products where id = $productid and enable = true");
}

/**
 * Get records count
 */
function get_row_count($table,$where_clause='')
{
	global $rd_db;
	
	$sql = "SELECT COUNT(*) total_size FROM $table where 1=1 ";
	
	if(!empty($where_clause))
		$sql = $sql.' and '.$where_clause;
	
	$ret = $rd_db->get_row($sql);
	return $ret['total_size'];
}

?>